import { defHttp } from '/@/common/util/axios';
import { ExceptionEnum } from '/@/common/enums/sysEnum';
import { useMessage } from '/@/hooks/web/useMessage';

const { createErrorModal } = useMessage();

enum Api {
    getAuthByTokenApi = '/sys/permission/getAuthByToken',

    loginApi = '/sys/login',                                    //登录
    phoneLoginApi = '/sys/phoneLogin',
    logoutApi = '/sys/logout',                                  //退出登录
    getRandomCodeApi = '/sys/randomImage',              //图形验证码  时间戳
    getCaptcha = '/sys/sms',                            //获取短信验证码
    registerApi = '/sys/user/register',                 //注册
    checkOnlyUser = '/sys/user/checkOnlyUser',          //校验用户
    validateCasLogin = '/sys/cas/client/validateLogin',   //SSO登录校验
    phoneVerify = '/sys/user/phoneVerification',        //校验手机号
    updatePassword4App = '/sys/user/updatePassword4App',    //修改密码
    thirdLogin = '/sys/thirdLogin/getLoginUser',        //第三方登录
    getThirdCaptcha = '/sys/thirdSms',                  //第三方登录
    getLoginQrcode = '/sys/getLoginQrcode',             //获取二维码信息  
    getQrcodeToken = '/sys/getQrcodeToken',             //监控二维码扫描状态
}

/**
 * 获取后台菜单权限和按钮权限
 */
export const getBackMenuAndPerms = () => defHttp.get({ url: Api.getAuthByTokenApi });

/**
 * 登录相关
 */
export const getLoginInfo = (params) => defHttp.post({ url: Api.loginApi, params });
export const doLoginByPhone = (params) => defHttp.post({ url: Api.phoneLoginApi, params },{joinParamsToUrl: true });
export const doLogout = () => defHttp.get({ url: Api.logoutApi });
export const getCodeInfo = (params) => defHttp.get({ url: Api.getRandomCodeApi + `/${params}` });
export const getLoginQrcode = () => defHttp.get({ url: Api.getLoginQrcode });
export const getQrcodeToken = (params) => defHttp.get({ url: Api.getQrcodeToken, params });
export const validateCasLogin = (params) => defHttp.get({ url: Api.validateCasLogin, params });

export const register = (params) => defHttp.get({ url: Api.registerApi, params }, { isReturnNativeResponse: true });
export const checkOnlyUser = (params) => defHttp.get({ url: Api.checkOnlyUser, params }, { isTransformResponse: false });
export const phoneVerify = (params) => defHttp.post({ url: Api.phoneVerify, params }, { isTransformResponse: false });
export const updatePassword4App = (params) => defHttp.get({ url: Api.updatePassword4App, params }, { isTransformResponse: false });

/**
 * 获取短信验证码
 */
export function getCaptcha(params) {
    return new Promise((resolve, reject) => {
        defHttp.post({ url: Api.getCaptcha, params }, { isTransformResponse: false }).then((res) => {
            console.log(res);
            if (res.success) {
                resolve(true);
            } else {
                //update-begin---author:wangshuai---date:2024-04-18---for:【QQYUN-9005】同一个IP，1分钟超过5次短信，则提示需要验证码---
                if (res.code != ExceptionEnum.PHONE_SMS_FAIL_CODE) {
                    createErrorModal({ title: '错误提示', content: res.message || '未知问题' });
                    reject();
                }
                reject(res);
                //update-end---author:wangshuai---date:2024-04-18---for:【QQYUN-9005】同一个IP，1分钟超过5次短信，则提示需要验证码---
            }
        }).catch((res) => {
            createErrorModal({ title: '错误提示', content: res.message || '未知问题' });
            reject();
        });
    });
}


